package com.pagoda.viewfinder_compiler.model;

import com.pagoda.viewfinder_annotation.BindClick;

import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Name;

/**
 * E-mail: liaohailong190@foxmail.com
 * Date: 2020/7/16 17:14
 * Description:
 *
 * @author liaohailong
 */
public class BindClickMethod {
    private ExecutableElement methodElement;
    private Name methodName;
    private int[] ids;

    public BindClickMethod(Element element) {
        if (element.getKind() != ElementKind.METHOD) {
            String message = String.format("Only methods can be annotated with @%s", BindClick.class.getSimpleName());
            throw new IllegalArgumentException(message);
        }

        this.methodElement = (ExecutableElement) element;
        ids = methodElement.getAnnotation(BindClick.class).value();

        if (ids.length < 1) {
            String message = String.format("Must set valid ids for @%s", BindClick.class.getSimpleName());
            throw new IllegalArgumentException(message);
        } else {
            for (int id : ids) {
                if (id < 0) {
                    String message = String.format("Must set valid ids for @%s", BindClick.class.getSimpleName());
                    throw new IllegalArgumentException(message);
                }
            }
        }

        methodName = methodElement.getSimpleName();
    }

    public int[] getIds() {
        return ids;
    }

    public Name getMethodName() {
        return methodName;
    }
}
